<?php

/**
 * This is the model class for table "goods_order".
 *
 * The followings are the available columns in table 'goods_order':
 * @property integer $id
 * @property integer $goodsid
 * @property integer $count
 * @property integer $state
 * @property integer $supplierid
 * @property string $ordertime
 * @property string $finishtime
 */
class GoodsOrder extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return GoodsOrder the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'goods_order';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('goodsid, count, state, supplierid', 'required'),
			array('goodsid, count, state, supplierid', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('goodsid','checkGoodsId'),
			array('id, goodsid, count, state, supplierid, ordertime, finishtime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'goodsinfo'=>array(self::BELONGS_TO,'Goods','goodsid'),
				'supplier'=>array(self::BELONGS_TO,'Supplier','supplierid')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'goodsid' => '货品ID',
			'count' => '订货数',
			'state' => 'State',
			'supplierid' => '供应商',
			'ordertime' => 'Ordertime',
			'finishtime' => 'Finishtime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('goodsid',$this->goodsid);
		$criteria->compare('count',$this->count);
		$criteria->compare('state',$this->state);
		$criteria->compare('supplierid',$this->supplierid);
		$criteria->compare('ordertime',$this->ordertime,true);
		$criteria->compare('finishtime',$this->finishtime,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function checkGoodsId($attribute,$params){
		$goodsModel = Goods::model();
		if($goodsModel->findByPk($this->goodsid)==null){
			$this->addError('goodsid','货物id不存在');
		}
	}
}